<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>新闻列表</cite></a>
        <a><cite>编辑新闻</cite></a>
    </div>
</div>
<div class="layui-fluid" id="baseApp">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form" lay-filter="layuiadmin-form-tags" id="layuiadmin-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <script type="text/html" template>
                            <input type="text" name="title" value="{{ d.params.title || '' }}" lay-verify="required"
                                   placeholder="请输入..." autocomplete="off" class="layui-input">
                        </script>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">文章内容</label>
                    <div class="layui-input-block">
                        <textarea class="layui-textarea " placeholder="请输内容"
                                  name="content" lay-verify="content"
                                  id="editor_demo"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"></label>
                    <div class="layui-input-inline">
                        <input type="button" lay-submit lay-filter="layuiadmin-submit" value="确认" class="layui-btn">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var editIndex;
    layui.use(['form', 'helper', 'laytpl', 'layedit'], function () {
        var form = layui.form,
            $ = layui.$,
            setter = layui.setter,
            helper = layui.helper;
        form.render('select');
        layedit = layui.layedit;
        layedit.set({
            tool: [
            'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'fontFomatt', 'colorpicker'
            , '|', 'left', 'center', 'right']
        })
        editIndex = layedit.build('editor_demo');
        var id = layui.router().search.id || '';
        helper.get('/app/news-index/edit?id=' + id, function (res) {
            form.val('layuiadmin-form-tags' , {
                title : res.data.title ,
            });
            layedit.setContent(editIndex , res.data.content);
        });
        form.on('submit(layuiadmin-submit)', function (data){
            let content = layedit.getContent(editIndex);
            if (!content){
                helper.error('请输入内容');
                return;
            }
            helper.post('/app/news-index/edit?id=' + id, {
                id : id ,
                content: content,
                title : data.field.title ,
            }, function (res) {
                helper.success(res.msg, function () {
                    editIndex = layedit.build('editor_demo');
                    window.location.href = '#/---news/';
                });
            });
            return false;
        });
    });
</script>
<style>
    .layui-card-header .layui-icon{
        line-height: 30px;
        position: relative;
        right: initial;
        top: initial;
        margin-top: initial;
    }
</style>